package com.tupu.service.impl.dispatch;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tupu.entity.dispatch.StandardizationRule;
import com.tupu.service.dispatch.StandardizationRuleService;
import com.tupu.mapper.dispatch.StandardizationRuleMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author 15702
 * @description 针对表【tb_standardization_rule】的数据库操作Service实现
 * @createDate 2023-11-13 10:12:11
 */
@Service
public class StandardizationRuleServiceImpl extends ServiceImpl<StandardizationRuleMapper, StandardizationRule> implements StandardizationRuleService {
    ConcurrentHashMap<String, List<StandardizationRule>> ruleConcurrentHashMap = new ConcurrentHashMap<>();

    @Override
    public List<StandardizationRule> getRuleCache(String voltageLevel, String deviceType) {
        List<StandardizationRule> ruleList = ruleConcurrentHashMap.get(voltageLevel + deviceType);
        if (ruleList == null) {
            ruleList = this.getRule(voltageLevel, deviceType);
            if (ruleList.size() == 0) {
                ruleList = this.getRule(voltageLevel, "公共");
            }
            ruleConcurrentHashMap.put(voltageLevel + deviceType, ruleList);
        }
        return ruleList;
    }
    @Override
    public void clearRuleCache() {
        ruleConcurrentHashMap.clear();
    }

    @Override
    public List<StandardizationRule> getRule(String voltageLevel, String deviceType) {
        QueryWrapper<StandardizationRule> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(voltageLevel) && StringUtils.isNotEmpty(deviceType)) {
            queryWrapper
                    .and(q -> q.likeRight("voltage_level", voltageLevel.trim())
                            .or()
                            .like("voltage_level", "/" + voltageLevel.trim())
                    ).like("type_keyword", deviceType.trim());
        } else {
            return this.baseMapper.selectList(queryWrapper);
        }
        return this.baseMapper.selectList(queryWrapper);
    }
}




